﻿using Admin.Crypto.Dto;
using Admin.Crypto.Session;
using Admin.Crypto.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Admin.Crypto.Controllers
{
    /// <summary>
    /// 加密解密联系数据接口
    /// </summary>
    [Route("[controller]/[action]")]
    [ApiController, Authorize]
    public class CryptoController : ControllerBase
    {
        private readonly ICustomerContactAppService appService;
        private readonly IUserSession userSession;
        private readonly ILogger logger;

        public CryptoController(ICustomerContactAppService appService, IUserSession userSession, ILoggerFactory factory)
        {
            this.appService = appService;
            this.userSession = userSession;
            logger = factory.CreateLogger<CryptoController>();
        }

        /// <summary>
        /// 创建加密的联系数据
        /// </summary>
        /// <param name="input">待加密数据</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultOutput<EncryptionOutput>> CreateAsync(string input)
        {           
            logger.LogDebug(JsonConvert.SerializeObject(userSession));
            return await appService.CreateAsync(input);
        }

        /// <summary>
        /// 创建加密的联系数据集合
        /// </summary>
        /// <param name="input">待加密数据集合</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultOutput<List<EncryptionOutput>>> CreateListAsync([FromBody] CreateListInput input)
        {
            return await appService.CreateListAsync(input);
        }

        /// <summary>
        /// 解密联系数据
        /// </summary>
        /// <param name="input">待解密唯一id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultOutput<DecryptionOutput>> DecryptAsync(string input)
        {
            return await appService.DecryptAsync(input);
        }

        /// <summary>
        /// 解密联系数据集合
        /// </summary>
        /// <param name="input">待解密id集合</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultOutput<List<DecryptionOutput>>> DecryptListAsync([FromBody] DecipherListInput input)
        {
            return await appService.DecryptListAsync(input);
        }

        /// <summary>
        /// 根据联系数据获取唯一ID
        /// </summary>
        /// <param name="input">联系数据</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultOutput<Guid>> GetIdAsync(string input)
        {
            return await appService.GetIdAsync(input);
        }

        /// <summary>
        /// 根据联系数据集合获取唯一ID集合
        /// </summary>
        /// <param name="input">联系数据集合</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultOutput<List<IdListOutput>>> GetIdListAsync([FromBody] GetIdsInput input)
        {
            return await appService.GetIdListAsync(input);
        }
    }
}
